from pygame import image as pi , mixer
from pygame import display as pdis


def create_image ( filename , alpha = 255 ) :
    image = pi.load ( filename ).convert ( )
    if not alpha == 255 :
        image = image.set_alpha ( alpha )
    return image


def create_image_alpha ( filename ) :
    return pi.load ( filename ).convert_alpha ( )


def center_x ( father_size , self_size , self_pos ) :
    return (father_size [ 0 ] - self_size [ 0 ]) / 2 , self_pos [ 1 ]


def center_y ( father_size , self_size , self_pos ) :
    return self_pos [ 0 ] , (father_size [ 1 ] - self_size [ 1 ]) / 2


def center ( father_size , self_size ) :
    return (father_size [ 0 ] - self_size [ 0 ]) / 2 , (father_size [ 1 ] - self_size [ 1 ]) / 2


def play_music ( filename ) :
    mixer.music.load ( filename )
    mixer.music.play ( -1 )


def screenshot ( ) :
    return pdis.get_surface ( )


def cut_image ( surface , width = 32 , height = 32 , type = 0 ) :
    width = int ( width )
    height = int ( height )
    from pygame.rect import Rect
    images = [ ]
    imgRect = surface.get_rect ( )
    col = imgRect.width / width
    row = imgRect.height / height
    if type == 0 :
        for x in range ( 0 , row ) :
            for y in range ( 0 , col ) :
                rect = Rect ( y * width , x * height , width , height )
                images.append ( surface.subsurface ( rect ) )
    else :
        for x in range ( 0 , col ) :
            for y in range ( 0 , row ) :
                rect = Rect ( x * width , y * height , width , height )
                images.append ( surface.subsurface ( rect ) )
    return images
